package com.example.onlineeducation.mapper

import com.example.onlineeducation.data.constant.ShowStatus
import com.example.onlineeducation.data.entity.BannerEntity
import com.example.onlineeducation.data.json.BannerJson
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycToNoEmpty
import com.yc.jetpacklib.mapper.IMapper

class BannerMapper : IMapper<List<BannerJson>?, List<BannerEntity>?> {
    override fun map(input: List<BannerJson>?): List<BannerEntity> {

        val list = mutableListOf<BannerEntity>()
        if (input.ycIsNotEmpty()) {
            for (item in input!!) {
                if (item.state == ShowStatus.SHOW) {
                    list.add(
                        BannerEntity(
                            item.mediaInfo?.imgUrl,
                            item.updateTime.ycToNoEmpty(),
                            item.jumpAddress,
                            item.name.ycToNoEmpty(),
                            item.position.ycToNoEmpty(Int.MAX_VALUE)
                        )
                    )
                }
            }
            // 列表排序
            val lengthComparator = Comparator { data1: BannerEntity, data2: BannerEntity ->
                data1.position - data2.position
            }
            return list.sortedWith(lengthComparator)

        } else {
            return list
        }

    }
}


